package com.zretc.controller;

import com.github.pagehelper.PageInfo;
import com.zretc.entity.Account;
import com.zretc.service.UserBackService;
import com.zretc.util.MD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import org.springframework.ui.Model;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/getUser")
public class UserBackController {

    private static final Logger logger = LoggerFactory.getLogger(UserBackController.class);

    @Autowired
    private UserBackService userBackService;

    @RequestMapping(value="/userPage",method = RequestMethod.GET)
    @ResponseBody
    public PageInfo<Account> getAccountList(@RequestParam(value = "pageNum") String pageNum,
                                            @RequestParam(value = "pageSize") String pageSize,
                                            @RequestParam(value = "accountName",required = false) String accountName){
        // 2.调用业务层
        Map<String, String> map = new HashMap<>();
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        map.put("accountName",accountName);

        PageInfo<Account> accountPage = userBackService.getAccountPage(map);

        return accountPage;

    }

    //@ResponseBody
    @RequestMapping("/insertUser")
    public String insertAccount(@RequestParam(value = "accountName") String accountName,
                                @RequestParam(value = "password") String password,
                                @RequestParam(value = "file") MultipartFile file){
                                // 单文件 MultipartFile接口定义数据类型 参数名和表单name一致 @RequestParam(value = "file") MultipartFile file
        logger.info(accountName);
        logger.info(password);

        password = MD5Util.getMd5(password);

        // 接收文件
        // file.getSize()==0
        if(file.isEmpty()){
            // 没有上传组件
            logger.info("====无文件上传====");
            // 调用方法
            userBackService.insertAccount(new Account(null, accountName, password, null));
        } else{
            // 1获取文件名
            String filename = file.getOriginalFilename();

            // 可以自定义文件名+文件后缀名
            // 1.1 获取文件后缀名
            String fileSuffix = filename.substring(filename.lastIndexOf("."));
            // 1.2 时间戳自定义文件名
            String dateStamp = System.currentTimeMillis() + "";
            // 1.3 存储文件名
            String newFileName = dateStamp + fileSuffix;

            // 调用方法
            userBackService.insertAccount(new Account(null,accountName,password,newFileName));

            // 2.将文件存储到指定目录中
            // 将文件存储在指定服务器中(本地电脑E盘)
            File f = new File("C:/image/" + newFileName); //  C:\image\文件名.后缀名

            try {
                file.transferTo(f);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        return "redirect:skip";

    }

    @ResponseBody
    @RequestMapping("/updateUser")
    public String updateAccount(@RequestParam(value = "updateAccountId") String updateAccountId,
                                @RequestParam(value = "updateAccountName") String updateAccountName,
                                @RequestParam(value = "updatePassword") String updatePassword,
                                @RequestParam(value = "updatefile") MultipartFile updatefile){

        // boolean updateAccount;
        // 接收文件
        // file.getSize()==0
        if(updatefile.isEmpty()){
            // 没有上传组件
            logger.info("====无文件上传====");
            // 调用方法
            userBackService.updateAccount(new Account(updateAccountId, updateAccountName, updatePassword, null));
        } else{
            // 1获取文件名
            String filename = updatefile.getOriginalFilename();

            // 可以自定义文件名+文件后缀名
            // 1.1 获取文件后缀名
            String fileSuffix = filename.substring(filename.lastIndexOf("."));
            // 1.2 时间戳自定义文件名
            String dateStamp = System.currentTimeMillis() + "";
            // 1.3 存储文件名
            String newFileName = dateStamp + fileSuffix;

            // 调用方法
            userBackService.updateAccount(new Account(updateAccountId,updateAccountName,updatePassword,newFileName));

            // 2.将文件存储到指定目录中
            // 将文件存储在指定服务器中(本地电脑E盘)
            File f = new File("C:/image/" + newFileName); //  C:\image\文件名.后缀名

            try {
                updatefile.transferTo(f);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        return "true";

    }

    //@ResponseBody
    @RequestMapping("/deleteUser")
    public String deleteAccount(@RequestParam(value = "deleteAccountId",required = false) String deleteAccountId
                                ) {

        // 调用方法
        boolean deleteByAccountId = userBackService.deleteByAccountId(deleteAccountId);

        // 判断
        if(deleteByAccountId == true){
            return "forward:userPage";
        }else{
            // 向 request 域中设置错误提示信息
            // model.addAttribute("deleteError","删除失败");
            return "forward:skipTo";
        }

    }

    @RequestMapping("/skip")
    public String skip(){
        return "redirect:../back/user-manage.jsp";
    }

    @RequestMapping("/skipTo")
    public String skipTo(){
        return "redirect:../back/user-manage.jsp";
    }

}
